package com.example.cloud.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.cloud.entities.Pay;
import com.example.cloud.entities.PayDTO;
import com.example.cloud.resp.ResultData;
import com.example.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-03-05
 * Time: 21:22
 */

@RestController
@Slf4j
@RequestMapping("/pay")
@Tag(name = "支付微服务模块",description = "支付CRUD")
public class PayController {

    @Resource
    private PayService payService;

    @PostMapping(value = "/add")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        log.info("addPay:{}", pay.toString());
        int add = payService.add(pay);
        return ResultData.success("成功插入记录，返回值：" + add);
    }

    @DeleteMapping(value = "/del/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public ResultData<Integer> delete(@PathVariable("id") int id) {
        return ResultData.success(payService.delete(id));
    }

    @PutMapping(value = "/update")
    @Operation(summary = "修改",description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        // 将 DTO 转实体
        Pay pay = new Pay();
        // hutool 工具包提供的方法，将 DTO 转实体
        BeanUtil.copyProperties(payDTO, pay);
        int i = payService.update(pay);
        return ResultData.success("成功修改记录，返回值：" + i);
    }


    @GetMapping(value = "/get/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public ResultData<Pay> geyById(@PathVariable("id") int id) {
        if (id <= 0) {
            throw new RuntimeException("查询的 id 信息错误");
        }
        return ResultData.success(payService.getById(id));
    }

    @GetMapping(value = "/getAll")
    @Operation(summary = "查询所有单子信息", description = "查询所有支付流水")
    public ResultData<List<Pay>> getAll() {
        return ResultData.success(payService.getAll());
    }


    @Value("${server.port}")
    private String port;


    @GetMapping("/get/info")
    public String getInfoByConsul(@Value("${wangshangg.info}") String info) throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        return "我是支付微服务，端口：" + port + "，info：" + info;
    }




}
